package host

import (
	"github.com/gin-gonic/gin"
	"inspur.com/cloudware/controller"
	"inspur.com/cloudware/model"
	"inspur.com/cloudware/serializer"
	hostService "inspur.com/cloudware/service/host"
	"inspur.com/cloudware/util/gconv"
)

// AddHost
func AddHost(c *gin.Context) {
	req := model.HostReq{}
	//post body映射代码对象
	if err := c.ShouldBind(&req); err == nil {
		_, err := hostService.AddHost(&req, c)
		if err == nil {
			c.JSON(200, serializer.Response{
				Code: 0,
				Msg:  "success",
			})
		} else {
			c.JSON(400, controller.ErrorResponse(err))
		}

	} else {
		c.JSON(400, controller.ErrorResponse(err))
	}
}

// RemoveHost
func RemoveHost(c *gin.Context) {
	hostId := gconv.Int64(c.Query("host_id"))
	rs := hostService.RemoveHostById(hostId)
	if rs > 0 {
		c.JSON(200, serializer.Response{
			Code: 0,
			Msg:  "success",
		})
	} else {
		c.JSON(400, serializer.Response{
			Code: 0,
			Msg:  "failed",
		})
	}

}

// UpdateHost
func UpdateHost(c *gin.Context) {
	c.JSON(400, serializer.Response{
		Code: 0,
		Msg:  "wuha",
	})
}

// GetHostByID
func GetHostByID(c *gin.Context) {
	if hostID := c.Query("hostid"); hostID != "" {
		host, err := model.GetHost(hostID)
		if err == nil {
			c.JSON(200, host)
		} else {
			c.JSON(400, serializer.Response{
				Code: 0,
				Msg:  "wuha",
			})
		}

	} else {
		c.JSON(400, serializer.Response{
			Code: 0,
			Msg:  "wuha",
		})
	}

}

func GetHostList(c *gin.Context) {
	i, hostList := model.GetHostList(1, 10000)
	if i != 0 {
		c.JSON(200, hostList)
	} else {
		c.JSON(400, serializer.Response{
			Code: 0,
			Msg:  "no data",
		})
	}
}
